﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace MonitorPlan.Admin
{
    public partial class Default :Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               if(Session["UserId"]!=null)
               {
                   Response.Redirect("/Admin/Location.aspx");
               }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string UserName = this.txtUserName.Text;
            string pass = CoreLibrary.CoreSecurity.getMd5Hash(this.txtPassword.Text);

            Model.User u = new Model.User().GetUser(UserName);

            if (u.UserName.ToString().ToLower() == UserName.ToLower())
            {
                if (u.Password == pass && u.UserType=="Admin")
                {
                    Session["UserId"] = u.Id;
                    Session["UserName"] = u.UserName;
                    Session["UserType"] = u.UserType;
                    Session["Email"] = u.Email;
                    Response.Redirect("/Admin/Location.aspx");
                }
                else
                {
                    this.loginSms.Text = "Invalid password. Try again!";
                }
            }
            else
            {
                this.loginSms.Text = "Invalid user name! Make you type the name correctly.";
            }
        }
    }
}